package com.gzdh.dishrecognition.ui.adapter.info;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.dhkj.web.bean.KlStateRes;
import com.gzdh.dishrecognition.AppContext;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.ui.widget.SwipeLayout;
import com.gzdh.dishrecognition.ui.widget.SwipeLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaoyehai on 2016/12/1.
 */
public class KlInfoAdapter extends BaseAdapter implements SwipeLayout.OnSwipeStateChangeListener {

    private Context context;
    private List<KlStateRes> list;

    private UserClick userClick;
    private TextView idTv;
    private TextView bmTv;
    private TextView nameTv;
    private TextView deleteBut;

    View view;
    LayoutInflater inflater;

    private View getViews() {
        LinearLayout mainLinear = new LinearLayout(context);
        mainLinear.setOrientation(LinearLayout.VERTICAL);
        mainLinear.setId(View.generateViewId());
        mainLinear.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        SwipeLayout swipeLayout = new SwipeLayout(context);
        swipeLayout.setId(View.generateViewId());
        swipeLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mainLinear.addView(swipeLayout);

        LinearLayout itemLinear = new LinearLayout(context);
        itemLinear.setId(View.generateViewId());
        itemLinear.setOrientation(LinearLayout.HORIZONTAL);
        itemLinear.setPadding(0, 10, 0, 10);
        itemLinear.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        swipeLayout.addView(itemLinear);

        idTv = new TextView(context);
        idTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        idTv.setGravity(Gravity.CENTER);
        idTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        itemLinear.addView(idTv);

        bmTv = new TextView(context);
        bmTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        bmTv.setGravity(Gravity.CENTER);
        bmTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        itemLinear.addView(bmTv);


        nameTv = new TextView(context);
        nameTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        nameTv.setGravity(Gravity.CENTER);
        nameTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        itemLinear.addView(nameTv);

        LinearLayout switchLayout = new LinearLayout(context);
        switchLayout.setId(View.generateViewId());
        switchLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
        switchLayout.setOrientation(LinearLayout.HORIZONTAL);
        swipeLayout.addView(switchLayout);

        deleteBut = new TextView(context);
        deleteBut.setText("删除");
        deleteBut.setLayoutParams(new LinearLayout.LayoutParams(100, ViewGroup.LayoutParams.MATCH_PARENT));
        deleteBut.setBackgroundColor(Color.RED);
        deleteBut.setTextColor(Color.YELLOW);
        deleteBut.setGravity(Gravity.CENTER);
        deleteBut.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        switchLayout.addView(deleteBut);

        return mainLinear;
    }

    public void setUserClick(UserClick userClick) {
        this.userClick = userClick;
    }

    public KlInfoAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();

    }

    public void setList(List<KlStateRes> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    public interface UserClick {
        void onUserClick(KlStateRes klStateRes);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = inflater.getContext();
        view = getViews();
        idTv.setText(list.get(position).getId());
        bmTv.setText(String.valueOf(list.get(position).getXuhao()));
        nameTv.setText(list.get(position).getKlName().trim());
        deleteBut.setOnClickListener(view1 -> {
            if (userClick != null) {
                userClick.onUserClick(list.get(position));
            }
        });
        return view;
    }

    @Override
    public void onOpen(Object tag) {
//        Toast.makeText(context, "第" + (Integer) tag + "个打开", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClose(Object tag) {
//        Toast.makeText(context, "第" + (Integer) tag + "个关闭", Toast.LENGTH_SHORT).show();
    }

}